package com.shxc.smarttrade.old.adapternew;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.InfomationBean;
import com.shxc.smarttrade.old.myinterface.RecyclerViewItemClickListener;
import com.shxc.smarttrade.old.myview.MLImageView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class ProductsAlertAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private RecyclerViewItemClickListener listener;
    private List<InfomationBean.DataBean> infomationBeanList;

    public ProductsAlertAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<InfomationBean.DataBean> infomationBeanList) {
        this.infomationBeanList = infomationBeanList;
        notifyDataSetChanged();
    }

    public void setMyItemCLickListener(RecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        if (viewType == 0)
        return new MasterHolder(mInflater.inflate(R.layout.adapter_products_alert, parent, false));
//        else
//            return new MoreHolder(mInflater.inflate(R.layout.adapter_click_load_more, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MasterHolder) {
            final InfomationBean.DataBean db = infomationBeanList.get(position);
            ((MasterHolder) holder).tvTitle.setText(db.getTitle());
            ((MasterHolder) holder).tvContent.setText(db.getIntroduce());
            ((MasterHolder) holder).tvComeFrom.setText("来源：" + db.getAuthor());
            Glide.with(mContext).load(db.getImgURL()).into(((MasterHolder) holder).imageView);
            ((MasterHolder) holder).tvTime.setText(db.getTime() + "");

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemCLick(position, db);
                }
            });
        }
//        else {
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                }
//            });
//        }

    }

    @Override
    public int getItemCount() {
        return infomationBeanList == null ? 0 : infomationBeanList.size();
    }

//    @Override
//    public int getItemViewType(int position) {
//        if (position == 5)
//            return 1;
//        else
//            return 0;
//    }

    public class MasterHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.image_view)
        MLImageView imageView;
        @BindView(R.id.tv_come_from)
        TextView tvComeFrom;
        @BindView(R.id.tv_time)
        TextView tvTime;

        MasterHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

//    public class MoreHolder extends RecyclerView.ViewHolder {
//        MoreHolder(View view) {
//            super(view);
//        }
//    }
}
